package com.zrf.base.knowledge.concurrent.demo;

import org.apache.commons.lang3.RandomUtils;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @date 2020/3/25
 * @AUTHER zhouruifeng
 */
public class SynchronousQueueDemo {
    public static void main(String[] args) {
        SynchronousQueue synchronousQueue = new SynchronousQueue();

        new Thread(()-> {
            try {
                System.out.println("start put 1");
                synchronousQueue.put("1");
                System.out.println("start put 2");
                synchronousQueue.put("2");
                System.out.println("start put 3");
                synchronousQueue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(()-> {
            try {
                System.out.println(synchronousQueue.take());
                TimeUnit.SECONDS.sleep(RandomUtils.nextInt(0,10));
                System.out.println(synchronousQueue.take());
                TimeUnit.SECONDS.sleep(RandomUtils.nextInt(0,10));
                System.out.println(synchronousQueue.take());
                TimeUnit.SECONDS.sleep(RandomUtils.nextInt(0,10));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
